package cn.livingCloud.dto;

import cn.livingCloud.entity.user.AccountPrivilege;
import cn.livingCloud.entity.user.Privilege;
import cn.livingCloud.entity.user.User;
import cn.livingCloud.entity.user.UserAccount;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 2015/12/4.
 */
public class UserAccountFormDto {
    protected String uuid;
    protected String userId;
    protected UserDto userDto;

    protected String username;
    protected String password;

    protected List<Privilege> privilegeList = new ArrayList<>();

    public UserAccountFormDto() {

    }

    public UserAccountFormDto(UserAccount userAccount) {
        this.uuid = userAccount.getUuid();
        this.userId = userAccount.getUserId();
        this.username = userAccount.getUsername();
        this.password = userAccount.getPassword();
        for (AccountPrivilege accountPrivilege : userAccount.getPrivilegeList()) {
            this.privilegeList.add(accountPrivilege.getPrivilege());
        }
        User user = userAccount.getUser();
        if (user != null) {
            this.userDto = new UserDto(user);
        }
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public UserDto getUserDto() {
        return userDto;
    }

    public UserAccountFormDto setUserDto(UserDto userDto) {
        this.userDto = userDto;
        return this;
    }

    public List<Privilege> getPrivilegeList() {
        return privilegeList;
    }

    public void setPrivilegeList(List<Privilege> privilegeList) {
        this.privilegeList = privilegeList;
    }

    public Privilege[] getAllPrivilege() {
        return Privilege.values();
    }

    public void updateAccount(UserAccount userAccount) {
        if (StringUtils.isEmpty(getUuid())) {
            userAccount.setPassword(this.getPassword());
        }
        userAccount.setUsername(this.getUsername().trim());
//        userAccount.setUserId(this.getUserId());
    }

    public void load(UserAccount userAccount) {
        this.uuid = userAccount.getUuid();
        this.userId = userAccount.getUserId();
        this.username = userAccount.getUsername();
        this.password = userAccount.getPassword();
        for (AccountPrivilege accountPrivilege : userAccount.getPrivilegeList()) {
            this.privilegeList.add(accountPrivilege.getPrivilege());
        }
    }
}
